package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.constant.AddressConstant;
import com.xueyi.aixihu.entity.generatedMapper.AxhFinanceMapper;
import com.xueyi.aixihu.entity.generatedMapper.DtNewsMapper;
import com.xueyi.aixihu.entity.po.*;
import com.xueyi.aixihu.entity.vo.ApiNewsSortVo;
import com.xueyi.aixihu.entity.vo.ApiSystemTypeContentVo;
import com.xueyi.aixihu.entity.vo.ApiSystemTypeVo;
import com.xueyi.aixihu.entity.vo.AxhFinanceVo;
import com.xueyi.aixihu.mapper.ApiSanWuGongKaiMapper;
import com.xueyi.aixihu.mapper.FinanceMapper;
import com.xueyi.aixihu.reqeust.ApiSearchSystemTypeContentReq;
import com.xueyi.aixihu.reqeust.NewsTypeReq;
import com.xueyi.aixihu.service.ApiSanWuGongKaiService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 描述
 *
 * @author 刘梓江
 * @date 2023-07-01 09:39
 */
@Service
@AllArgsConstructor
public class ApiSanWuGongKaiServiceImpl implements ApiSanWuGongKaiService {

    private final ApiSanWuGongKaiMapper apiSanWuGongKaiMapper;


    @Override
    public List<ApiNewsSortVo> searchVillageCommunity() {

        //初始化信息
        Map<Integer, ApiNewsSortVo> apiNewsSortVoMAp = new HashMap<>();
        apiNewsSortVoMAp.put(AddressConstant.CUN, new ApiNewsSortVo(AddressConstant.CUN, "村", new ArrayList<>()));
        apiNewsSortVoMAp.put(AddressConstant.SQ, new ApiNewsSortVo(AddressConstant.SQ, "社区", new ArrayList<>()));
        apiNewsSortVoMAp.put(AddressConstant.JW, new ApiNewsSortVo(AddressConstant.JW, "居委", new ArrayList<>()));

        //查询所有社区
        List<DtNewsSort> dtNewsSorts = apiSanWuGongKaiMapper.selectVillageCommunity(Arrays.asList(AddressConstant.CUN, AddressConstant.SQ, AddressConstant.JW));
        if (!CollectionUtils.isEmpty(dtNewsSorts)) {
            Map<Integer, List<DtNewsSort>> collect = dtNewsSorts.stream().collect(Collectors.groupingBy(e -> e.getPid(), Collectors.toList()));

            //二次封装
            collect.forEach(
                    (key, value) -> {
                        ApiNewsSortVo apiNewsSortVo = apiNewsSortVoMAp.get(key);
                        apiNewsSortVo.setChild(value);
                    }
            );
        }
        return apiNewsSortVoMAp.values().stream().collect(Collectors.toList());
    }

    @Override
    public List<ApiSystemTypeVo> searchSystemType(Integer type) {
        Map<Integer, ApiSystemTypeVo> map = new HashMap<>();
        if (type == 1) {
            map.put(1, new ApiSystemTypeVo(1, "党务公开", new ArrayList<>()));
            map.put(2, new ApiSystemTypeVo(2, "政务公开", new ArrayList<>()));
            map.put(3, new ApiSystemTypeVo(3, "财务公开", new ArrayList<>()));
        } else if (type == 2) {
            map.put(4, new ApiSystemTypeVo(4, "党务公开", new ArrayList<>()));
            map.put(5, new ApiSystemTypeVo(5, "村务公开", new ArrayList<>()));
            map.put(6, new ApiSystemTypeVo(6, "财务公开", new ArrayList<>()));
        } else {
            map.put(3, new ApiSystemTypeVo(3, "资金管理", new ArrayList<>()));
            map.put(4, new ApiSystemTypeVo(4, "资源管理", new ArrayList<>()));
            map.put(5, new ApiSystemTypeVo(5, "资产管理", new ArrayList<>()));
        }

        if (type != 3) {
            //查询类型列表
            List<NewsTypeReq> newsTypeReqs = apiSanWuGongKaiMapper.selectSystemTypeByType(type);

            //按照父进行分组
            Map<Integer, List<NewsTypeReq>> collect = newsTypeReqs.stream().collect(Collectors.groupingBy(e -> e.getPid(), Collectors.toList()));
            System.out.println(collect.entrySet().stream().filter(e1 -> e1.getKey() != 0).collect(Collectors.toList()));
            //封装分及子内容
            collect.entrySet().stream().filter(e1 -> e1.getKey() != 0).forEach(
                    (e1) -> {
                            ApiSystemTypeVo apiSystemTypeVo = map.get(e1.getKey());
                            apiSystemTypeVo.setChild(e1.getValue().stream().sorted(Comparator.comparing(NewsTypeReq::getCreateTime).reversed()).collect(Collectors.toList()));
                    }
            );
        }
        return map.values().stream().sorted(Comparator.comparing((ApiSystemTypeVo::getId))).collect(Collectors.toList());
    }

    @Override
    public List<ApiSystemTypeContentVo> searchSystemTypeContent(ApiSearchSystemTypeContentReq req) {
        //计算分页位置
        req.countStartIndex();
        //初始化内容容器
        List<ApiSystemTypeContentVo> contentVos = new ArrayList<>();

        if (req.getSearchType() == 1) {

            //查询文章
            List<DtNewsPo> dtNews = apiSanWuGongKaiMapper.selectDtNewsBySystemId(req);
            if(!CollectionUtils.isEmpty(dtNews)){

                //获取文章id
                StringJoiner ids=new StringJoiner(",");
                dtNews.stream().map(e->e.getId()).collect(Collectors.toList()).forEach(e->ids.add(e.toString()));
                //获取文章最后一次操作时间新列表，并转换成map
                Map<Integer, Date> newReleaseTimeMap = apiSanWuGongKaiMapper.selectNewsLastPublicRecord(ids.toString()).stream().collect(Collectors.toMap(CdzAuditRecord::getId, CdzAuditRecord::getSubmitTime));

                //获取来源信息
                List<Integer> formAreaIds=dtNews.stream().map(e->e.getFromToAreaId()).collect(Collectors.toList());;
                Map<Integer, String> dtNewsMap = apiSanWuGongKaiMapper.selectNewsSortById(formAreaIds).stream().collect(Collectors.toMap(DtNewsSort::getId, DtNewsSort::getName));
                dtNews.forEach(
                        e -> {
                            //封装内容
                            contentVos.add(new ApiSystemTypeContentVo(e.getId(), e.getTitle(), e.getContent(),newReleaseTimeMap.get(e.getId()), null,"",dtNewsMap.get(e.getFromToAreaId())));
                        }
                );

            }
        } else {
            if (req.getSearchType() > 2) {
                req.setSearchType(req.getSearchType() - 2);
            }
            //查询报表
            List<AxhFinanceVo> axhFinanceVos = apiSanWuGongKaiMapper.selectFinanceBySystemId(req);
            if(!CollectionUtils.isEmpty(axhFinanceVos)){

                StringJoiner ids=new StringJoiner(",");
                axhFinanceVos.stream().map(e->e.getId()).collect(Collectors.toList()).forEach(e->ids.add(e.toString()));
                Map<Integer, Date>  financeReleaseTimeMap  = apiSanWuGongKaiMapper.selectFinanceLastPublicRecord(ids.toString()).stream().collect(Collectors.toMap(AxhFinanceLog::getFinanceId, AxhFinanceLog::getCreateTime));

                //获取来源信息
                List<Integer> formAreaIds=axhFinanceVos.stream().map(e->e.getFromToAreaId()).collect(Collectors.toList());;
                Map<Integer, String> dtNewsMap = apiSanWuGongKaiMapper.selectNewsSortById(formAreaIds).stream().collect(Collectors.toMap(DtNewsSort::getId, DtNewsSort::getName));
                axhFinanceVos.forEach(
                        e -> {
                            String fileType=e.getSourceFileUrl().substring(e.getSourceFileUrl().lastIndexOf(".")+1);
                            contentVos.add(new ApiSystemTypeContentVo(e.getId(), e.getFinanceTitle(), e.getSourceFileUrl(), financeReleaseTimeMap.get(e.getId()), e.getReportProofImage(),fileType,dtNewsMap.get(e.getFromToAreaId())));
                        }
                );
            }

        }
        return contentVos;
    }

    @Override
    public ApiSystemTypeContentVo searchStatementContentDesc(Integer id, Integer searchType) {
        if (searchType == 1) {
            DtNewsPo e = apiSanWuGongKaiMapper.selectDtNewsById(id);
            List<CdzAuditRecord> cdzAuditRecord = apiSanWuGongKaiMapper.selectNewsLastPublicRecord(id.toString());
            Date date=cdzAuditRecord.get(0).getSubmitTime();
            List<DtNewsSort> dtNewsSort = apiSanWuGongKaiMapper.selectNewsSortById(Arrays.asList(e.getFromToAreaId()));
            return new ApiSystemTypeContentVo(e.getId(), e.getTitle(), e.getContent(), date, "","",dtNewsSort.get(0).getName());
        } else {
            AxhFinanceVo e = apiSanWuGongKaiMapper.selectFinanceById(id);
            List<AxhFinanceLog> axhFinanceLogs = apiSanWuGongKaiMapper.selectFinanceLastPublicRecord(id.toString());
            Date date=axhFinanceLogs.get(0).getCreateTime();
            List<DtNewsSort> dtNewsSort = apiSanWuGongKaiMapper.selectNewsSortById(Arrays.asList(e.getFromToAreaId()));
            String fileType=e.getSourceFileUrl().substring(e.getSourceFileUrl().lastIndexOf(".")+1);
            return new ApiSystemTypeContentVo(e.getId(), e.getFinanceTitle(), e.getSourceFileUrl(), date, e.getReportProofImage(),fileType,dtNewsSort.get(0).getName());
        }
    }
}
